﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Text;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class EdiLogBLL
    {
        private static readonly IAlternateSkuDA alternateSkuDA = DataAccess.CreateAlternateSkuDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA customerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private static readonly IEdiLogDA ediLogDA = DataAccess.CreateEdiLogDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly IUserMstDA userMstDA = DataAccess.CreateUserMstDA();
        private static readonly IUserVendorDA userVendorDA = DataAccess.CreateUserVendorDA();
        private static readonly IVendorDA vendorDA = DataAccess.CreateVendorDA();

        public EdiLogBLL()
        {
            this.pubParasInfo = null;
        }

        public EdiLogBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public EdiLogBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string ASNImportByEDI_YITAI(List<string> data, string ownerID, string vendorID)
        {
            string result = "";
            string sOldRow = "";
            string sRow = "";
            string sOldExternalReceiptID = "";
            string sPriorReceiptID = "";
            string whLoginID = this.dataBaseID[0];
            int lineID = 10;
            List<ReceiptInfo> receiptList = new List<ReceiptInfo>();
            ReceiptInfo receiptInfo = new ReceiptInfo();
            List<ReceiptDetailInfo> receiptDetailList = new List<ReceiptDetailInfo>();
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                int i;
                if (ownerDA.GetTradePartnerByID(dataBase, tran, ownerID, whLoginID) == null)
                {
                    result = result + string.Format("货主{0}不存在!<br />", ownerID);
                }
                VendorInfo vendorInfo = vendorDA.GetVendorByID(dataBase, tran, vendorID, whLoginID);
                if (vendorInfo == null)
                {
                    result = result + string.Format("供应商{0}不存在!<br />", vendorID);
                }
                for (i = 0; i < data.Count; i++)
                {
                    sRow = data[i].Replace(" ", "");
                    if (string.Compare(sOldRow, sRow, true) != 0)
                    {
                        sOldRow = sRow;
                        if (!string.IsNullOrEmpty(sRow))
                        {
                            DateTime CS00003;
                            string[] values = sRow.Split(new char[] { '\t' });
                            if (sOldExternalReceiptID != values[1])
                            {
                                if (receiptDA.GetReceiptByExternalID(dataBase, tran, values[1], whLoginID, this.pubParasInfo) != null)
                                {
                                    result = result + string.Format("第{0}行外部订单号{1}已存在!<br />", i + 1, values[1]);
                                }
                                if (!((receiptInfo == null) || string.IsNullOrEmpty(receiptInfo.ReceiptID)))
                                {
                                    receiptList.Add(receiptInfo);
                                    sPriorReceiptID = receiptInfo.ReceiptID;
                                }
                                receiptInfo = new ReceiptInfo();
                                receiptDetailList = new List<ReceiptDetailInfo>();
                                lineID = 10;
                                receiptInfo.ReceiptID = commonMethod.GenNextNumStr(dataBase, tran, "RECEIPT", whLoginID);
                                receiptInfo.ReceiptType = values[0];
                                receiptInfo.OwnerID = ownerID;
                                receiptInfo.PoID = "";
                                receiptInfo.ExternalReceiptID = values[1];
                                receiptInfo.PackingSlipNumber = "";
                                receiptInfo.Rma = "";
                                receiptInfo.ReceiptDate = DateTime.Now;
                                receiptInfo.ExpectedReceiptDate = DateTime.Now;
                                CS00003 = new DateTime();
                                receiptInfo.ClosedDate = CS00003;
                                receiptInfo.OpenQty = 0M;
                                receiptInfo.Status = "New";
                                receiptInfo.ExternalReceiptId2 = "";
                                receiptInfo.WarehouseRef = "";
                                receiptInfo.ArrivalDate = DateTime.Today;
                                receiptInfo.AdviceNumber = "";
                                CS00003 = new DateTime();
                                receiptInfo.AdviceDate = CS00003;
                                receiptInfo.TransportationMode = "LTL";
                                if (vendorInfo != null)
                                {
                                    receiptInfo.VendorID = vendorInfo.VendorID;
                                    receiptInfo.VendorName = vendorInfo.Company;
                                    receiptInfo.VendorAddress = vendorInfo.Address;
                                    receiptInfo.VendorCity = vendorInfo.City;
                                    receiptInfo.VendorProv = vendorInfo.State;
                                    receiptInfo.VendorZip = vendorInfo.Zip;
                                    receiptInfo.VendorCountry = vendorInfo.Country;
                                    receiptInfo.VendorContact = vendorInfo.Contact1;
                                    receiptInfo.VendorFax = vendorInfo.Fax1;
                                    receiptInfo.VendorPhone = vendorInfo.Phone1;
                                    receiptInfo.VendorEmail = vendorInfo.Email1;
                                }
                                receiptInfo.VendorRef = "";
                                receiptInfo.CarrierID = "";
                                receiptInfo.CarrierName = "";
                                receiptInfo.CarrierAddress = "";
                                receiptInfo.CarrierCity = "";
                                receiptInfo.CarrierProv = "";
                                receiptInfo.CarrierZip = "";
                                receiptInfo.CarrierCountry = "";
                                receiptInfo.CarrierContact = "";
                                receiptInfo.CarrierPhone = "";
                                receiptInfo.CarrierFax = "";
                                receiptInfo.CarrierEmail = "";
                                receiptInfo.CarrierRef = "";
                                receiptInfo.DriverName = "";
                                receiptInfo.TrailerNumber = "";
                                receiptInfo.TrailerOwner = "";
                                receiptInfo.SrcCountry = "";
                                receiptInfo.DestCountry = "";
                                receiptInfo.VehicleNumber = "";
                                CS00003 = new DateTime();
                                receiptInfo.VehicleDate = CS00003;
                                receiptInfo.PlaceOfLoading = "";
                                receiptInfo.PlaceOfDischarge = "";
                                receiptInfo.PlaceOfDelivery = "";
                                receiptInfo.IncoTerms = "CFR";
                                receiptInfo.PaymentTerm = "Cash";
                                receiptInfo.ContainerRef = "";
                                receiptInfo.ContainerType = "";
                                receiptInfo.ContainerQty = 0M;
                                receiptInfo.Udf1 = values[15];
                                receiptInfo.Udf2 = "";
                                receiptInfo.Udf3 = "";
                                receiptInfo.Udf4 = "";
                                receiptInfo.Udf5 = "";
                                receiptInfo.Udf6 = "";
                                receiptInfo.Udf7 = "";
                                receiptInfo.Udf8 = "";
                                receiptInfo.Udf9 = "";
                                receiptInfo.Udf10 = "";
                                receiptInfo.AllowAutoReceipt = "";
                                receiptInfo.TrackInventoryBy = "";
                                receiptInfo.LottableMatchRequired = "N";
                                receiptInfo.IsPalletized = "N";
                                receiptInfo.Remark = "";
                                receiptInfo.IsActive = "N";
                                receiptInfo.CreatedBy = this.pubParasInfo.UserID;
                                receiptInfo.CreatedDate = DateTime.Now;
                                receiptInfo.UpdatedBy = this.pubParasInfo.UserID;
                                receiptInfo.UpdatedDate = DateTime.Now;
                                receiptInfo.CoreReleaseID = "";
                                receiptInfo.IsDeclartion = "N";
                                receiptInfo.ShipName = "";
                                receiptInfo.Voyage = "";
                                receiptInfo.DestinationPort = "";
                                receiptInfo.DeparturePort = "";
                                receiptInfo.DeclarationsType = "";
                                receiptInfo.WorkOrder = "";
                                receiptInfo.SAPAuthentication = "";
                            }
                            sOldExternalReceiptID = values[1];
                            string skuID = values[8];
                            string skuDescr = values[10];
                            string skuDescr2 = values[9];
                            decimal qty = string.IsNullOrEmpty(values[12]) ? 0M : decimal.Parse(values[12]);
                            string lot06 = values[4];
                            ReceiptDetailInfo receiptDetailInfo = new ReceiptDetailInfo {
                                ReceiptID = receiptInfo.ReceiptID,
                                ReceiptLineID = lineID,
                                ExternReceiptID = receiptInfo.ExternalReceiptID,
                                ExternLineID = lineID.ToString(),
                                OwnerID = ownerID,
                                PoID = "",
                                PoLineID = 0,
                                TariffID = "",
                                SkuID = skuID,
                                SkuDescr = skuDescr,
                                LpnID = "",
                                CaseID = "",
                                Status = "New",
                                ReceivedDate = DateTime.Now,
                                ExpectedQty = qty,
                                AdjustedQty = 0M,
                                ReceivedQty = 0M,
                                RejectedQty = 0M,
                                ToLoc = "",
                                ToLot = "",
                                ToLpnID = "",
                                ConditionCode = "OK",
                                LotAttr01 = "",
                                LotAttr02 = "",
                                LotAttr03 = "",
                                LotAttr04 = "",
                                LotAttr05 = "",
                                LotAttr06 = lot06,
                                LotAttr07 = "",
                                LotAttr08 = "",
                                LotAttr09 = "OK",
                                LotAttr10 = ""
                            };
                            CS00003 = new DateTime();
                            receiptDetailInfo.ProduceDate = CS00003;
                            CS00003 = new DateTime();
                            receiptDetailInfo.ExpiryDate = CS00003;
                            receiptDetailInfo.ReceiptDate = DateTime.Now;
                            receiptDetailInfo.ExternalLot = "";
                            receiptDetailInfo.EffectiveDate = DateTime.Now;
                            receiptDetailInfo.ReasonCode = "";
                            receiptDetailInfo.PalletID = "";
                            receiptDetailInfo.QcRequired = "N";
                            receiptDetailInfo.QcInspectedQty = 0M;
                            receiptDetailInfo.QcRejectedQty = 0M;
                            receiptDetailInfo.QcAutoAdjust = "N";
                            receiptDetailInfo.VendorID = receiptInfo.VendorID;
                            receiptDetailInfo.VendorName = receiptInfo.VendorName;
                            receiptDetailInfo.DeclarationQty = 0M;
                            receiptDetailInfo.CreatedBy = receiptInfo.CreatedBy;
                            receiptDetailInfo.CreatedDate = receiptInfo.CreatedDate;
                            receiptDetailInfo.UpdatedBy = receiptInfo.UpdatedBy;
                            receiptDetailInfo.UpdatedDate = receiptInfo.UpdatedDate;
                            ReceiptLotInfo receiptLot = new ReceiptLotInfo(receiptDetailInfo.LotAttr01, receiptDetailInfo.LotAttr02, receiptDetailInfo.LotAttr03, receiptDetailInfo.LotAttr04, receiptDetailInfo.LotAttr05, receiptDetailInfo.LotAttr06, receiptDetailInfo.LotAttr07, receiptDetailInfo.LotAttr08, receiptDetailInfo.LotAttr09, receiptDetailInfo.LotAttr10, receiptDetailInfo.ProduceDate, receiptDetailInfo.ExpiryDate, receiptDetailInfo.ReceiptDate, receiptDetailInfo.ExternalLot, receiptDetailInfo.VendorID);
                            receiptDetailInfo.ReceiptLot = receiptLot;
                            SkuInfo skuInfo = skuDA.GetSkuByID(dataBase, tran, ownerID, skuID, whLoginID);
                            if (skuInfo != null)
                            {
                                if ((skuInfo.Descr != skuDescr) || (skuInfo.Descr2 != skuDescr2))
                                {
                                    skuInfo.Descr = skuDescr;
                                    skuInfo.Descr2 = skuDescr2;
                                    skuInfo.UpdatedBy = receiptInfo.UpdatedBy;
                                    skuInfo.UpdatedDate = receiptInfo.UpdatedDate;
                                    skuDA.UpdateSku(dataBase, tran, skuInfo, this.dataBaseID);
                                }
                                if ((skuInfo.AlternateSkuInfos != null) && (skuInfo.AlternateSkuInfos.Count > 0))
                                {
                                    receiptDetailInfo.AltSkuID = skuInfo.AlternateSkuInfos[0].AltSkuID;
                                }
                                else
                                {
                                    receiptDetailInfo.AltSkuID = "";
                                }
                                receiptDetailInfo.Length = skuInfo.Length;
                                receiptDetailInfo.Width = skuInfo.Width;
                                receiptDetailInfo.Height = skuInfo.Height;
                                receiptDetailInfo.Cube = skuInfo.Cube;
                                receiptDetailInfo.GrossWgt = skuInfo.GrossWgt;
                                receiptDetailInfo.NetWgt = skuInfo.NetWgt;
                                receiptDetailInfo.UnitPrice = (float) skuInfo.CostPrice;
                                receiptDetailInfo.ExtendedPrice = (float) (skuInfo.CostPrice * qty);
                                PackInfo packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                                receiptDetailInfo.UomID = packInfo.PackUom1;
                                receiptDetailInfo.PackID = packInfo.PackID;
                                receiptDetailInfo.InnerPack = (decimal) packInfo.InnerPack;
                                receiptDetailInfo.CaseCnt = (decimal) packInfo.CaseCnt;
                                receiptDetailInfo.Pallet = (decimal) packInfo.Pallet;
                                receiptDetailInfo.OtherUnit1 = packInfo.OtherUnit1;
                                receiptDetailInfo.OtherUnit2 = packInfo.OtherUnit2;
                            }
                            else
                            {
                                result = result + string.Format("第{0}行货品{1}不存在!<br />", i + 1, skuID);
                            }
                            receiptDetailList.Add(receiptDetailInfo);
                            receiptInfo.ReceiptDetailInfos = receiptDetailList;
                            lineID += 10;
                        }
                    }
                }
                if (sPriorReceiptID != receiptInfo.ReceiptID)
                {
                    receiptList.Add(receiptInfo);
                }
                if (string.IsNullOrEmpty(result) && (receiptList.Count > 0))
                {
                    for (i = 0; i < receiptList.Count; i++)
                    {
                        receiptDA.InsertReceipt(dataBase, tran, receiptList[i], whLoginID);
                        auditDataBLL.InsertAuditData<ReceiptInfo>(dataBase, tran, null, receiptList[i], AuditAction.Insert, this.pubParasInfo.UserID, "RECEIPT", whLoginID);
                        if ((receiptList[i].ReceiptDetailInfos != null) && (receiptList[i].ReceiptDetailInfos.Count > 0))
                        {
                            for (int j = 0; j < receiptList[i].ReceiptDetailInfos.Count; j++)
                            {
                                receiptDetailDA.InsertReceiptDetail(dataBase, tran, receiptList[i].ReceiptDetailInfos[j], whLoginID);
                                auditDataBLL.InsertAuditData<ReceiptDetailInfo>(dataBase, tran, null, receiptList[i].ReceiptDetailInfos[j], AuditAction.Insert, this.pubParasInfo.UserID, "RECEIPT_DETAIL", whLoginID);
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                result = result + string.Format(" Error:{0}", ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public string DeleteEdiLog(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (ediLogDA.DeleteEdiLog(dataBase, tran, IDS) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllEdiLog()
        {
            return ediLogDA.GetAllEdiLog();
        }

        public object GetEdiLogByID(string ID)
        {
            return ediLogDA.GetEdiLogByID(ID);
        }

        public DataSet GetEdiLogByQueryList(EdiLogQueryEntity ediLogQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(ediLogQueryEntity.WhID))
            {
                sqlWhere.Add(" WH_ID = '" + ediLogQueryEntity.WhID + "'");
            }
            return ediLogDA.GetEdiLogByQueryList(sqlWhere, ediLogQueryEntity);
        }

        public DataSet GetEdiLogByWhere(List<string> sqlWhere, EdiLogQueryEntity ediLogQueryEntity)
        {
            return ediLogDA.GetEdiLogByQueryList(sqlWhere, ediLogQueryEntity);
        }

        public bool InsertEdiLog(EdiLogInfo ediLogInfo)
        {
            bool result = false;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (ediLogDA.InsertEdiLog(dataBase, tran, ediLogInfo) > 0)
                {
                    result = true;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public bool InsertEdiLogs(List<EdiLogInfo> ediLogInfos)
        {
            bool result = false;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (ediLogDA.InsertEdiLogs(dataBase, tran, ediLogInfos) > 0)
                {
                    result = true;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public DataSet InvExportByEDI_SkyTrans(string ownerID, string dbLoginID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT il.OWNER_ID, il.SKU_ID, la.VENDOR_ID, la.LOT_ATTR08,  ");
            sb.Append("   SUM(il.QTY - il.PREALLOCATED_QTY - il.ALLOCATED_QTY - il.PICKED_QTY - il.ONHOLD_QTY) AS QTY, ");
            sb.Append("   SUM(il.PREALLOCATED_QTY + il.ALLOCATED_QTY + il.PICKED_QTY + il.ONHOLD_QTY) AS HOLD_QTY ");
            sb.Append(" FROM @_@INV_LOT il ");
            sb.Append("   LEFT JOIN @_@LOT_ATTRIBUTE la ON la.OWNER_ID = il.OWNER_ID AND la.SKU_ID = il.SKU_ID AND la.LOT = il.LOT ");
            sb.Append(" WHERE la.LOT_ATTR09 = 'OK' AND il.OWNER_ID = '@OWNER_ID' ");
            sb.Append(" GROUP BY il.OWNER_ID, il.SKU_ID, la.VENDOR_ID, la.LOT_ATTR08 ");
            string sql = sb.Replace("@_@", dbLoginID + ".").Replace("@OWNER_ID", ownerID).ToString();
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public string OrderImportByEDI_SkyTrans(List<string> data, string ownerID, string dbLoginID, char splitChar)
        {
            string result = "";
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                string whName = dataBase.ExecuteDataSet(tran, CommandType.Text, string.Format("SELECT WH_DB_ID, WH_DB_NAME, WH_DB_DESC, DB_LOGIN_ID, DB_LOGIN_PASSWD FROM sysadm.DB_CONFIG WHERE IS_GLOBAL = 'N' AND DB_LOGIN_ID = '{0}' ORDER BY WH_DB_ID", dbLoginID)).Tables[0].Rows[0]["WH_DB_DESC"].ToString();
                string whLoginID = dbLoginID + ".";
                if (ownerDA.GetTradePartnerByID(dataBase, tran, ownerID, whLoginID) != null)
                {
                    int lineID = 10;
                    OrdersInfo orderInfo = new OrdersInfo();
                    List<OrderDetailInfo> orderDetailInfoList = new List<OrderDetailInfo>();
                    for (int i = 0; i < (data.Count - 1); i++)
                    {
                        DateTime CS00003;
                        string[] line = data[i].Split(new char[] { splitChar });
                        if (i == 0)
                        {
                            orderInfo.OrderID = commonMethod.GenNextNumStr(dataBase, tran, "ORDERS", whLoginID);
                            orderInfo.OwnerID = ownerID;
                            orderInfo.OrderDate = DateTime.Now;
                            orderInfo.RequestedShipDate = DateTime.Now;
                            CS00003 = new DateTime();
                            orderInfo.ActualShipDate = CS00003;
                            orderInfo.DeliveryDate = DateTime.Now;
                            CS00003 = new DateTime();
                            orderInfo.DeliveryDate2 = CS00003;
                            orderInfo.Priority = "3";
                            orderInfo.Type = "Standard";
                            orderInfo.OrderGroup = "";
                            orderInfo.OpenQty = 0M;
                            orderInfo.Status = "New";
                            orderInfo.ExternOrderID = "";
                            orderInfo.ExternalOrderId2 = "";
                            orderInfo.ReferenceNum = line[4].Trim();
                            orderInfo.TradingPartner = "";
                            orderInfo.ReceiptID = "";
                            orderInfo.ConsigneeID = "";
                            orderInfo.CCompany = "";
                            orderInfo.CAddress = "";
                            orderInfo.CCity = "";
                            orderInfo.CProv = "";
                            orderInfo.CZip = "";
                            orderInfo.CCountry = "";
                            orderInfo.CCountryCode = "";
                            orderInfo.CVat = "";
                            orderInfo.CContact1 = "";
                            orderInfo.CContact2 = "";
                            orderInfo.CEmail1 = "";
                            orderInfo.CEmail2 = "";
                            orderInfo.CPhone1 = "";
                            orderInfo.CPhone2 = "";
                            orderInfo.CFax1 = "";
                            orderInfo.CFax2 = "";
                            orderInfo.BillToID = "";
                            orderInfo.BCompany = "";
                            orderInfo.BAddress = "";
                            orderInfo.BCity = "";
                            orderInfo.BState = "";
                            orderInfo.BZip = "";
                            orderInfo.BCountry = "";
                            orderInfo.BCountryCode = "";
                            orderInfo.BContact1 = "";
                            orderInfo.BContact2 = "";
                            orderInfo.BEmail1 = "";
                            orderInfo.BEmail2 = "";
                            orderInfo.BPhone1 = "";
                            orderInfo.BPhone2 = "";
                            orderInfo.BFax1 = "";
                            orderInfo.BFax2 = "";
                            orderInfo.BVat = "";
                            orderInfo.CarrierID = "";
                            orderInfo.CarrierName = "";
                            orderInfo.CarrierAddress = "";
                            orderInfo.CarrierCity = "";
                            orderInfo.CarrierProv = "";
                            orderInfo.CarrierZip = "";
                            orderInfo.CarrierCountry = "";
                            orderInfo.CarrierCountryCode = "";
                            orderInfo.CarrierPhone = "";
                            orderInfo.DepartureDate = DateTime.Now;
                            orderInfo.TrailerNumber = "";
                            orderInfo.TrailerOwner = "";
                            orderInfo.DriverName = "";
                            orderInfo.ProNumber = "";
                            orderInfo.Door = "";
                            orderInfo.Route = "";
                            orderInfo.Stop = "";
                            orderInfo.Stage = "";
                            orderInfo.TransportationMode = "";
                            orderInfo.TransportationService = "";
                            orderInfo.IncoTerms = "";
                            orderInfo.PaymentTerm = "Cash";
                            orderInfo.ContainerType = "";
                            orderInfo.ContainerQty = 0M;
                            orderInfo.LoadID = "";
                            orderInfo.ExternalLoadID = "";
                            orderInfo.CountryOfOrigin = "";
                            orderInfo.CountryDestination = "";
                            orderInfo.DischargePlace = "";
                            orderInfo.DeliveryPlace = "";
                            orderInfo.HandlingType = "";
                            orderInfo.BatchFlag = "N";
                            orderInfo.ShipTogether = "N";
                            orderInfo.SortationLocation = "";
                            orderInfo.OrderBreak = "";
                            orderInfo.EnablePacking = "N";
                            orderInfo.Udf1 = "";
                            orderInfo.Udf2 = "";
                            orderInfo.Udf3 = "";
                            orderInfo.Udf4 = "";
                            orderInfo.Udf5 = "";
                            orderInfo.Udf6 = "";
                            orderInfo.Udf7 = "";
                            orderInfo.Udf8 = "";
                            orderInfo.Udf9 = "";
                            orderInfo.Udf10 = "";
                            orderInfo.BuyerPo = "";
                            orderInfo.OrderValue = 0M;
                            orderInfo.IntransitID = "";
                            orderInfo.UpdateSource = "";
                            orderInfo.SplitOrders = "N";
                            orderInfo.ApptStatus = "";
                            orderInfo.Remark = "";
                            orderInfo.Remark2 = "";
                            orderInfo.IsActive = "Y";
                            orderInfo.CreatedBy = "EDI";
                            orderInfo.CreatedDate = DateTime.Now;
                            orderInfo.UpdatedBy = "EDI";
                            orderInfo.UpdatedDate = DateTime.Now;
                        }
                        OrderDetailInfo orderDetailInfo = new OrderDetailInfo {
                            LineID = lineID,
                            ExternOrderID = orderInfo.ExternOrderID,
                            OwnerID = orderInfo.OwnerID,
                            SkuID = line[6].Trim(),
                            OriginalQty = string.IsNullOrEmpty(line[7].Trim()) ? 0M : decimal.Parse(line[7].Trim()),
                            VendorID = line[9].Trim(),
                            ExternalLot = ""
                        };
                        CS00003 = new DateTime();
                        orderDetailInfo.ProduceDate = CS00003;
                        CS00003 = new DateTime();
                        orderDetailInfo.ExpiryDate = CS00003;
                        orderDetailInfo.LotAttr01 = "";
                        orderDetailInfo.LotAttr02 = "";
                        orderDetailInfo.LotAttr03 = "";
                        orderDetailInfo.LotAttr04 = "";
                        orderDetailInfo.LotAttr05 = "";
                        orderDetailInfo.LotAttr06 = "";
                        orderDetailInfo.LotAttr07 = "";
                        orderDetailInfo.LotAttr08 = line[10].Trim();
                        orderDetailInfo.LotAttr09 = "";
                        orderDetailInfo.LotAttr10 = "";
                        orderDetailInfo.Udf1 = line[2].Trim();
                        orderDetailInfo.Udf2 = line[3].Trim();
                        orderDetailInfo.Udf3 = line[5].Trim();
                        orderDetailInfo.Udf4 = line[8].Trim();
                        orderDetailInfo.Udf5 = line[11].Trim();
                        orderDetailInfo.Udf6 = line[12].Trim();
                        orderDetailInfo.ExternLineID = line[5].Trim();
                        orderDetailInfo.AltSkuID = "";
                        orderDetailInfo.OpenQty = 0M;
                        orderDetailInfo.ShippedQty = 0M;
                        orderDetailInfo.AdjustedQty = 0M;
                        orderDetailInfo.PreallocatedQty = 0M;
                        orderDetailInfo.AllocatedQty = 0M;
                        orderDetailInfo.PickedQty = 0M;
                        orderDetailInfo.PickCode = "";
                        orderDetailInfo.LpnID = "";
                        orderDetailInfo.Status = "New";
                        orderDetailInfo.Tax01 = 0.0;
                        orderDetailInfo.Tax02 = 0.0;
                        orderDetailInfo.UnitPrice = 0.0;
                        orderDetailInfo.ExtendedPrice = 0.0;
                        orderDetailInfo.UpdateSource = "0";
                        orderDetailInfo.QcRequired = "0";
                        orderDetailInfo.QcAutoAdjust = "0";
                        orderDetailInfo.ShipGroup01 = "N";
                        orderDetailInfo.ShipGroup02 = "N";
                        orderDetailInfo.ShipGroup03 = "N";
                        orderDetailInfo.IsSubstitute = 0;
                        orderDetailInfo.OkToSubstitute = 0;
                        orderDetailInfo.Lot = "";
                        orderDetailInfo.EffectiveDate = DateTime.Now;
                        orderDetailInfo.OrigCaseQty = 0M;
                        orderDetailInfo.OrigPalletQty = 0M;
                        orderDetailInfo.QtyIntransit = 0M;
                        orderDetailInfo.WpReleased = "0";
                        orderDetailInfo.MinShipperCent = 0;
                        orderDetailInfo.PreallocateRuleID = "STD";
                        orderDetailInfo.AllocateRuleID = "STD";
                        orderDetailInfo.SkuRotation = "STD";
                        SkuInfo skuInfo = skuDA.GetSkuByID(orderDetailInfo.OwnerID, orderDetailInfo.SkuID, whLoginID);
                        if (skuInfo != null)
                        {
                            PackInfo packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                            orderDetailInfo.SkuDescr = skuInfo.Descr;
                            orderDetailInfo.PackID = skuInfo.PackID;
                            orderDetailInfo.UomID = packInfo.PackUom1;
                            orderDetailInfo.PreallocateRuleID = skuInfo.PreallocateRuleID;
                            orderDetailInfo.AllocateRuleID = skuInfo.AllocateRuleID;
                            orderDetailInfo.SkuRotation = skuInfo.RotationRuleID;
                            orderDetailInfo.CartonGroup = skuInfo.CartonGroup;
                            orderDetailInfo.ProductWeight = skuInfo.GrossWgt * double.Parse(orderDetailInfo.OriginalQty.ToString());
                            orderDetailInfo.ProductCube = skuInfo.Cube * double.Parse(orderDetailInfo.OriginalQty.ToString());
                            if (!string.IsNullOrEmpty(skuInfo.PreallocateRuleID))
                            {
                                orderDetailInfo.PreallocateRuleID = skuInfo.PreallocateRuleID;
                            }
                            if (!string.IsNullOrEmpty(skuInfo.AllocateRuleID))
                            {
                                orderDetailInfo.AllocateRuleID = skuInfo.AllocateRuleID;
                            }
                            if (!string.IsNullOrEmpty(skuInfo.RotationRuleID))
                            {
                                orderDetailInfo.SkuRotation = skuInfo.RotationRuleID;
                            }
                        }
                        else
                        {
                            result = result + string.Format("第{0}行货品{1}不存在!<br />", i + 1, orderDetailInfo.SkuID);
                        }
                        orderDetailInfo.CreatedBy = orderInfo.CreatedBy;
                        orderDetailInfo.CreatedDate = orderInfo.CreatedDate;
                        orderDetailInfo.UpdatedBy = orderInfo.UpdatedBy;
                        orderDetailInfo.UpdatedDate = orderInfo.UpdatedDate;
                        orderDetailInfoList.Add(orderDetailInfo);
                        orderInfo.OrderDetailInfos = orderDetailInfoList;
                        lineID += 10;
                    }
                    if (((string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(orderInfo.OrderID)) && (!string.IsNullOrEmpty(orderInfo.ReferenceNum) && (orderInfo.OrderDetailInfos != null))) && (orderInfo.OrderDetailInfos.Count > 0))
                    {
                        string sql = string.Format("SELECT ORDER_ID, [STATUS] FROM {0}ORDERS WHERE OWNER_ID = '{1}' AND REFERENCE_NUM = '{2}'", whLoginID, orderInfo.OwnerID, orderInfo.ReferenceNum);
                        DataSet ds = dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
                        if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                        {
                            result = result + string.Format("发货订单:货主{0},销售订单号{1}已存在", ownerID, orderInfo.ReferenceNum);
                        }
                        else
                        {
                            ordersDA.InsertOrders(dataBase, tran, orderInfo, whLoginID);
                            foreach (OrderDetailInfo orderDetail in orderDetailInfoList)
                            {
                                orderDetail.OrderID = orderInfo.OrderID;
                                orderDetailDA.InsertOrderDetail(dataBase, tran, orderDetail, whLoginID);
                            }
                        }
                    }
                }
                else
                {
                    result = result + string.Format("仓库:{0},货主:{1}不存在.", whName, ownerID);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                result = result + string.Format(" Error:{0}", ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public string OrderImportByEDI_YITAI(List<string> data, string ownerID)
        {
            string result = "";
            string sOldRow = "";
            string sRow = "";
            string sOldExternalOrderID = "";
            string sPriorOrderID = "";
            string whLoginID = this.dataBaseID[0];
            int lineID = 10;
            List<OrdersInfo> ordersList = new List<OrdersInfo>();
            OrdersInfo ordersInfo = new OrdersInfo();
            List<OrderDetailInfo> orderDetailList = new List<OrderDetailInfo>();
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                int i;
                if (ownerDA.GetTradePartnerByID(dataBase, tran, ownerID, whLoginID) == null)
                {
                    result = result + string.Format("货主{0}不存在!<br />", ownerID);
                }
                for (i = 0; i < data.Count; i++)
                {
                    sRow = data[i].Replace(" ", "");
                    if (string.Compare(sOldRow, sRow, true) != 0)
                    {
                        sOldRow = sRow;
                        if (!string.IsNullOrEmpty(sRow))
                        {
                            DateTime CS00003;
                            string[] values = sRow.Split(new char[] { '\t' });
                            if (sOldExternalOrderID != values[1])
                            {
                                if (ordersDA.GetOrdersByExternalID(dataBase, tran, values[1], whLoginID, this.pubParasInfo) != null)
                                {
                                    result = result + string.Format("第{0}行外部订单号{1}已存在!<br />", i + 1, values[1]);
                                }
                                if (!((ordersInfo == null) || string.IsNullOrEmpty(ordersInfo.OrderID)))
                                {
                                    ordersList.Add(ordersInfo);
                                    sPriorOrderID = ordersInfo.OrderID;
                                }
                                ordersInfo = new OrdersInfo();
                                orderDetailList = new List<OrderDetailInfo>();
                                lineID = 10;
                                CustomerInfo customerInfo = customerDA.GetCustomerByID(dataBase, tran, values[5], whLoginID, this.pubParasInfo);
                                if (customerInfo == null)
                                {
                                    result = result + string.Format("客户{0}不存在!<br />", values[5]);
                                }
                                else if (values[6] != customerInfo.Company)
                                {
                                    customerInfo.Company = values[6];
                                    customerDA.UpdateCustomer(dataBase, tran, customerInfo, this.dataBaseID);
                                }
                                ordersInfo.OrderID = commonMethod.GenNextNumStr(dataBase, tran, "ORDERS", whLoginID);
                                ordersInfo.OwnerID = ownerID;
                                ordersInfo.OrderDate = DateTime.Now;
                                ordersInfo.RequestedShipDate = DateTime.Now;
                                CS00003 = new DateTime();
                                ordersInfo.ActualShipDate = CS00003;
                                ordersInfo.DeliveryDate = DateTime.Now;
                                CS00003 = new DateTime();
                                ordersInfo.DeliveryDate2 = CS00003;
                                ordersInfo.Priority = "3";
                                ordersInfo.Type = "Standard";
                                ordersInfo.OrderGroup = "";
                                ordersInfo.OpenQty = 0M;
                                ordersInfo.Status = "New";
                                ordersInfo.ExternOrderID = values[1];
                                ordersInfo.ExternalOrderId2 = "";
                                ordersInfo.ReferenceNum = "";
                                ordersInfo.TradingPartner = "";
                                ordersInfo.ReceiptID = "";
                                if (customerInfo != null)
                                {
                                    ordersInfo.ConsigneeID = customerInfo.CustomerID;
                                    ordersInfo.CCompany = customerInfo.Company;
                                    ordersInfo.CAddress = customerInfo.Address;
                                    ordersInfo.CCity = customerInfo.City;
                                    ordersInfo.CProv = customerInfo.State;
                                    ordersInfo.CZip = customerInfo.Zip;
                                    ordersInfo.CCountry = customerInfo.Country;
                                    ordersInfo.CCountryCode = customerInfo.CountryCode;
                                    ordersInfo.CVat = customerInfo.Vat;
                                    ordersInfo.CContact1 = customerInfo.Contact1;
                                    ordersInfo.CContact2 = customerInfo.Contact2;
                                    ordersInfo.CEmail1 = customerInfo.Email1;
                                    ordersInfo.CEmail2 = customerInfo.Email2;
                                    ordersInfo.CPhone1 = customerInfo.Phone1;
                                    ordersInfo.CPhone2 = customerInfo.Phone2;
                                    ordersInfo.CFax1 = customerInfo.Fax1;
                                    ordersInfo.CFax2 = customerInfo.Fax2;
                                }
                                else
                                {
                                    ordersInfo.ConsigneeID = "";
                                    ordersInfo.CCompany = "";
                                    ordersInfo.CAddress = "";
                                    ordersInfo.CCity = "";
                                    ordersInfo.CProv = "";
                                    ordersInfo.CZip = "";
                                    ordersInfo.CCountry = "";
                                    ordersInfo.CCountryCode = "";
                                    ordersInfo.CVat = "";
                                    ordersInfo.CContact1 = "";
                                    ordersInfo.CContact2 = "";
                                    ordersInfo.CEmail1 = "";
                                    ordersInfo.CEmail2 = "";
                                    ordersInfo.CPhone1 = "";
                                    ordersInfo.CPhone2 = "";
                                    ordersInfo.CFax1 = "";
                                    ordersInfo.CFax2 = "";
                                }
                                ordersInfo.BillToID = "";
                                ordersInfo.BCompany = "";
                                ordersInfo.BAddress = "";
                                ordersInfo.BCity = "";
                                ordersInfo.BState = "";
                                ordersInfo.BZip = "";
                                ordersInfo.BCountry = "";
                                ordersInfo.BCountryCode = "";
                                ordersInfo.BContact1 = "";
                                ordersInfo.BContact2 = "";
                                ordersInfo.BEmail1 = "";
                                ordersInfo.BEmail2 = "";
                                ordersInfo.BPhone1 = "";
                                ordersInfo.BPhone2 = "";
                                ordersInfo.BFax1 = "";
                                ordersInfo.BFax2 = "";
                                ordersInfo.BVat = "";
                                ordersInfo.CarrierID = "";
                                ordersInfo.CarrierName = "";
                                ordersInfo.CarrierAddress = "";
                                ordersInfo.CarrierCity = "";
                                ordersInfo.CarrierProv = "";
                                ordersInfo.CarrierZip = "";
                                ordersInfo.CarrierCountry = "";
                                ordersInfo.CarrierCountryCode = "";
                                ordersInfo.CarrierPhone = "";
                                ordersInfo.DepartureDate = DateTime.Now;
                                ordersInfo.TrailerNumber = "";
                                ordersInfo.TrailerOwner = "";
                                ordersInfo.DriverName = "";
                                ordersInfo.ProNumber = "";
                                ordersInfo.Door = "";
                                ordersInfo.Route = "";
                                ordersInfo.Stop = "";
                                ordersInfo.Stage = "";
                                ordersInfo.TransportationMode = "";
                                ordersInfo.TransportationService = "";
                                ordersInfo.IncoTerms = "";
                                ordersInfo.PaymentTerm = "Cash";
                                ordersInfo.ContainerType = "";
                                ordersInfo.ContainerQty = 0M;
                                ordersInfo.LoadID = "";
                                ordersInfo.ExternalLoadID = "";
                                ordersInfo.CountryOfOrigin = "";
                                ordersInfo.CountryDestination = "";
                                ordersInfo.DischargePlace = "";
                                ordersInfo.DeliveryPlace = "";
                                ordersInfo.HandlingType = "";
                                ordersInfo.BatchFlag = "N";
                                ordersInfo.ShipTogether = "N";
                                ordersInfo.SortationLocation = "";
                                ordersInfo.OrderBreak = "";
                                ordersInfo.EnablePacking = "N";
                                ordersInfo.Udf1 = values[15];
                                ordersInfo.Udf2 = "";
                                ordersInfo.Udf3 = "";
                                ordersInfo.Udf4 = "";
                                ordersInfo.Udf5 = "";
                                ordersInfo.Udf6 = "";
                                ordersInfo.Udf7 = "";
                                ordersInfo.Udf8 = "";
                                ordersInfo.Udf9 = "";
                                ordersInfo.Udf10 = "";
                                ordersInfo.BuyerPo = "";
                                ordersInfo.OrderValue = 0M;
                                ordersInfo.IntransitID = "";
                                ordersInfo.UpdateSource = "";
                                ordersInfo.SplitOrders = "N";
                                ordersInfo.ApptStatus = "";
                                ordersInfo.Remark = "";
                                ordersInfo.Remark2 = "";
                                ordersInfo.IsActive = "Y";
                                ordersInfo.CreatedBy = this.pubParasInfo.UserID;
                                ordersInfo.CreatedDate = DateTime.Now;
                                ordersInfo.UpdatedBy = this.pubParasInfo.UserID;
                                ordersInfo.UpdatedDate = DateTime.Now;
                            }
                            sOldExternalOrderID = values[1];
                            string skuID = values[8];
                            string skuDescr = values[10];
                            string skuDescr2 = values[9];
                            decimal qty = string.IsNullOrEmpty(values[12]) ? 0M : decimal.Parse(values[12]);
                            OrderDetailInfo orderDetailInfo = new OrderDetailInfo {
                                OrderID = ordersInfo.OrderID,
                                LineID = lineID,
                                ExternOrderID = ordersInfo.ExternOrderID,
                                OwnerID = ownerID,
                                SkuID = skuID,
                                OriginalQty = qty,
                                VendorID = "",
                                ExternalLot = ""
                            };
                            CS00003 = new DateTime();
                            orderDetailInfo.ProduceDate = CS00003;
                            CS00003 = new DateTime();
                            orderDetailInfo.ExpiryDate = CS00003;
                            orderDetailInfo.LotAttr01 = "";
                            orderDetailInfo.LotAttr02 = "";
                            orderDetailInfo.LotAttr03 = "";
                            orderDetailInfo.LotAttr04 = "";
                            orderDetailInfo.LotAttr05 = "";
                            orderDetailInfo.LotAttr06 = "";
                            orderDetailInfo.LotAttr07 = "";
                            orderDetailInfo.LotAttr08 = "";
                            orderDetailInfo.LotAttr09 = "";
                            orderDetailInfo.LotAttr10 = "";
                            orderDetailInfo.Udf1 = "";
                            orderDetailInfo.Udf2 = "";
                            orderDetailInfo.Udf3 = "";
                            orderDetailInfo.Udf4 = "";
                            orderDetailInfo.Udf5 = "";
                            orderDetailInfo.Udf6 = "";
                            orderDetailInfo.Udf7 = "";
                            orderDetailInfo.Udf8 = "";
                            orderDetailInfo.Udf9 = "";
                            orderDetailInfo.Udf10 = "";
                            orderDetailInfo.ExternLineID = "";
                            orderDetailInfo.AltSkuID = "";
                            orderDetailInfo.OpenQty = 0M;
                            orderDetailInfo.ShippedQty = 0M;
                            orderDetailInfo.AdjustedQty = 0M;
                            orderDetailInfo.PreallocatedQty = 0M;
                            orderDetailInfo.AllocatedQty = 0M;
                            orderDetailInfo.PickedQty = 0M;
                            orderDetailInfo.PickCode = "";
                            orderDetailInfo.LpnID = "";
                            orderDetailInfo.Status = "New";
                            orderDetailInfo.Tax01 = 0.0;
                            orderDetailInfo.Tax02 = 0.0;
                            orderDetailInfo.UnitPrice = 0.0;
                            orderDetailInfo.ExtendedPrice = 0.0;
                            orderDetailInfo.UpdateSource = "0";
                            orderDetailInfo.QcRequired = "0";
                            orderDetailInfo.QcAutoAdjust = "0";
                            orderDetailInfo.ShipGroup01 = "N";
                            orderDetailInfo.ShipGroup02 = "N";
                            orderDetailInfo.ShipGroup03 = "N";
                            orderDetailInfo.IsSubstitute = 0;
                            orderDetailInfo.OkToSubstitute = 0;
                            orderDetailInfo.Lot = "";
                            orderDetailInfo.EffectiveDate = DateTime.Now;
                            orderDetailInfo.OrigCaseQty = 0M;
                            orderDetailInfo.OrigPalletQty = 0M;
                            orderDetailInfo.QtyIntransit = 0M;
                            orderDetailInfo.WpReleased = "0";
                            orderDetailInfo.MinShipperCent = 0;
                            orderDetailInfo.PreallocateRuleID = "STD";
                            orderDetailInfo.AllocateRuleID = "STD";
                            orderDetailInfo.SkuRotation = "STD";
                            orderDetailInfo.CreatedBy = ordersInfo.CreatedBy;
                            orderDetailInfo.CreatedDate = ordersInfo.CreatedDate;
                            orderDetailInfo.UpdatedBy = ordersInfo.UpdatedBy;
                            orderDetailInfo.UpdatedDate = ordersInfo.UpdatedDate;
                            SkuInfo skuInfo = skuDA.GetSkuByID(dataBase, tran, ownerID, skuID, whLoginID);
                            if (skuInfo != null)
                            {
                                if ((skuInfo.Descr != skuDescr) || (skuInfo.Descr2 != skuDescr2))
                                {
                                    skuInfo.Descr = skuDescr;
                                    skuInfo.Descr2 = skuDescr2;
                                    skuInfo.UpdatedBy = ordersInfo.UpdatedBy;
                                    skuInfo.UpdatedDate = ordersInfo.UpdatedDate;
                                    skuDA.UpdateSku(dataBase, tran, skuInfo, this.dataBaseID);
                                }
                                if ((skuInfo.AlternateSkuInfos != null) && (skuInfo.AlternateSkuInfos.Count > 0))
                                {
                                    orderDetailInfo.AltSkuID = skuInfo.AlternateSkuInfos[0].AltSkuID;
                                }
                                orderDetailInfo.Length = skuInfo.Length;
                                orderDetailInfo.Width = skuInfo.Width;
                                orderDetailInfo.Height = skuInfo.Height;
                                orderDetailInfo.Cube = skuInfo.Cube;
                                orderDetailInfo.GrossWGT = skuInfo.GrossWgt;
                                orderDetailInfo.NetWGT = skuInfo.NetWgt;
                                orderDetailInfo.UnitPrice = (double) skuInfo.CostPrice;
                                orderDetailInfo.ExtendedPrice = (double) (skuInfo.CostPrice * qty);
                                orderDetailInfo.CartonGroup = skuInfo.CartonGroup;
                                orderDetailInfo.ProductCube = skuInfo.Cube * ((double) qty);
                                orderDetailInfo.ProductWeight = skuInfo.GrossWgt * ((double) qty);
                                PackInfo packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                                orderDetailInfo.UomID = packInfo.PackUom1;
                                orderDetailInfo.PackID = packInfo.PackID;
                            }
                            else
                            {
                                result = result + string.Format("第{0}行货品{1}不存在!<br />", i + 1, skuID);
                            }
                            orderDetailList.Add(orderDetailInfo);
                            ordersInfo.OrderDetailInfos = orderDetailList;
                            lineID += 10;
                        }
                    }
                }
                if (sPriorOrderID != ordersInfo.OrderID)
                {
                    ordersList.Add(ordersInfo);
                }
                if (string.IsNullOrEmpty(result) && (ordersList.Count > 0))
                {
                    for (i = 0; i < ordersList.Count; i++)
                    {
                        ordersDA.InsertOrders(dataBase, tran, ordersList[i], whLoginID);
                        auditDataBLL.InsertAuditData<OrdersInfo>(dataBase, tran, null, ordersList[i], AuditAction.Insert, this.pubParasInfo.UserID, "ORDERS", whLoginID);
                        if ((ordersList[i].OrderDetailInfos != null) && (ordersList[i].OrderDetailInfos.Count > 0))
                        {
                            for (int j = 0; j < ordersList[i].OrderDetailInfos.Count; j++)
                            {
                                orderDetailDA.InsertOrderDetail(dataBase, tran, ordersList[i].OrderDetailInfos[j], whLoginID);
                                auditDataBLL.InsertAuditData<OrderDetailInfo>(dataBase, tran, null, ordersList[i].OrderDetailInfos[j], AuditAction.Insert, this.pubParasInfo.UserID, "ORDER_DETAIL", whLoginID);
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                result = result + string.Format(" Error:{0}", ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public DataSet ShipmentExportByEDI_SkyTrans(string ownerID, string dbLoginID)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT o.ORDER_ID, o.REFERENCE_NUM, od.UDF1 AS OD_UDF1, od.UDF2 AS OD_UDF2, od.UDF3 AS OD_UDF3, od.UDF4 AS OD_UDF4, od.UDF5 AS OD_UDF5, od.UDF6 AS OD_UDF6, pd.SKU_ID, la.VENDOR_ID, la.LOT_ATTR08, SUM(pd.QTY) AS QTY ");
            sb.Append(" FROM @_@PICK_DETAIL pd  ");
            sb.Append("   LEFT JOIN @_@ORDER_DETAIL od ON pd.ORDER_ID = od.ORDER_ID AND pd.ORDER_LINE_ID = od.LINE_ID ");
            sb.Append("   LEFT JOIN @_@ORDERS o ON o.ORDER_ID = pd.ORDER_ID ");
            sb.Append("   LEFT JOIN @_@LOT_ATTRIBUTE la ON pd.OWNER_ID = la.OWNER_ID AND pd.SKU_ID = la.SKU_ID AND pd.LOT = la.LOT ");
            sb.Append(" WHERE o.[STATUS] IN ('Shipped','Closed') AND o.EDI_SYNC_FLG <> 'Y' AND o.CREATED_BY = 'EDI' AND o.OWNER_ID = '@OWNER_ID' ");
            sb.Append(" GROUP BY o.ORDER_ID, o.REFERENCE_NUM, od.UDF1, od.UDF2, od.UDF3, od.UDF4, od.UDF5, od.UDF6, pd.SKU_ID, la.VENDOR_ID, la.LOT_ATTR08 ");
            sb.Append(" ORDER BY o.REFERENCE_NUM, od.UDF3 ");
            string sql = sb.Replace("@_@", dbLoginID + ".").Replace("@OWNER_ID", ownerID).ToString();
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public string SkuImportByEDI_SkyTrans(List<string> data, string ownerID, string dbLoginID, char splitChar)
        {
            string result = "";
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DataSet dsWH = dataBase.ExecuteDataSet(tran, CommandType.Text, "SELECT WH_DB_ID, WH_DB_NAME, WH_DB_DESC, DB_LOGIN_ID, DB_LOGIN_PASSWD FROM sysadm.DB_CONFIG WHERE IS_GLOBAL = 'N' ORDER BY WH_DB_ID");
                foreach (DataRow row in dsWH.Tables[0].Rows)
                {
                    string whName = row["WH_DB_DESC"].ToString();
                    string whLoginID = row["DB_LOGIN_ID"].ToString() + ".";
                    TradePartnerInfo ownerInfo = ownerDA.GetTradePartnerByID(dataBase, tran, ownerID, whLoginID);
                    if (ownerInfo != null)
                    {
                        DataSet dsPack;
                        string PackID = "STD";
                        string DefaultUom = "EA";
                        using (dsPack = dataBase.ExecuteDataSet(tran, CommandType.Text, string.Format("SELECT TOP 1 PACK_ID, PACK_UOM1 FROM {0}PACK", whLoginID)))
                        {
                            PackID = dsPack.Tables[0].Rows[0]["PACK_ID"].ToString();
                            DefaultUom = dsPack.Tables[0].Rows[0]["PACK_UOM1"].ToString();
                        }
                        for (int i = 0; i < (data.Count - 1); i++)
                        {
                            string[] line = data[i].Split(new char[] { splitChar });
                            SkuInfo skuInfo = new SkuInfo {
                                OwnerID = ownerID,
                                SkuID = line[2]
                            };
                            if (skuDA.CheckSkuIDUnique(dataBase, tran, ownerID, skuInfo.SkuID, whLoginID))
                            {
                                skuInfo = skuDA.GetSkuByID(dataBase, tran, ownerID, skuInfo.SkuID, whLoginID);
                                skuInfo.Descr = line[3].Trim();
                                skuInfo.Udf1 = line[11].Trim();
                                skuInfo.Udf2 = line[12].Trim();
                                skuInfo.Udf3 = line[0x16].Trim();
                                skuInfo.Udf4 = line[0x17].Trim();
                                skuInfo.UpdatedBy = "EDI";
                                skuInfo.UpdatedDate = DateTime.Now;
                                using (dsPack = dataBase.ExecuteDataSet(tran, CommandType.Text, string.Format("SELECT TOP 1 PACK_ID, PACK_UOM1 FROM {0}PACK WHERE PACK_ID = '{1}'", whLoginID, skuInfo.PackID)))
                                {
                                    PackID = dsPack.Tables[0].Rows[0]["PACK_ID"].ToString();
                                    DefaultUom = dsPack.Tables[0].Rows[0]["PACK_UOM1"].ToString();
                                }
                                List<string> gInitLocal0 = new List<string> {
                                    whLoginID
                                };
                                skuDA.UpdateSku(dataBase, tran, skuInfo, gInitLocal0);
                            }
                            else
                            {
                                skuInfo.Descr = line[3].Trim();
                                skuInfo.Descr2 = "";
                                skuInfo.PackID = !string.IsNullOrEmpty(ownerInfo.DefaultPackID) ? ownerInfo.DefaultPackID : PackID;
                                skuInfo.CartonGroup = !string.IsNullOrEmpty(ownerInfo.DefaultCartonGroup) ? ownerInfo.DefaultCartonGroup : "STD";
                                skuInfo.LotTemplateID = !string.IsNullOrEmpty(ownerInfo.DefaultLotTemplateID) ? ownerInfo.DefaultLotTemplateID : "STD";
                                skuInfo.TariffID = "STD";
                                skuInfo.SkuClassID = "";
                                skuInfo.HazmatCode = "";
                                skuInfo.CustomSkuNo = "";
                                skuInfo.CountryOfOrigin = "";
                                skuInfo.ShelfLifeIndicator = "N";
                                skuInfo.ShelfLifeCodeType = "E";
                                skuInfo.ShelfLifeOnReceiving = 0;
                                skuInfo.ShelfLife = 0;
                                skuInfo.DaysToExpire = 0;
                                skuInfo.Length = 0f;
                                skuInfo.Width = 0f;
                                skuInfo.Height = 0f;
                                skuInfo.Cube = 0f;
                                skuInfo.GrossWgt = 0f;
                                skuInfo.NetWgt = 0f;
                                skuInfo.TareWgt = 0f;
                                skuInfo.SkuSize = "";
                                skuInfo.SkuColor = "";
                                skuInfo.SkuStyle = "";
                                skuInfo.CostPrice = 0M;
                                skuInfo.SalePrice = 0M;
                                skuInfo.NetPrice = 0M;
                                skuInfo.IsSalePromption = "N";
                                skuInfo.ReceiptValidationTemplate = "";
                                skuInfo.DefaultInUom = DefaultUom;
                                skuInfo.DefaultOutUom = DefaultUom;
                                skuInfo.RfDefaultPack = !string.IsNullOrEmpty(ownerInfo.DefaultPackID) ? ownerInfo.DefaultPackID : PackID;
                                skuInfo.PutawayRuleID = ownerInfo.DefaultPutawayRuleID;
                                skuInfo.PreallocateRuleID = ownerInfo.DefaultPreallocateRuleID;
                                skuInfo.AllocateRuleID = ownerInfo.DefaultAllocateRuleID;
                                skuInfo.RotationRuleID = ownerInfo.DefaultRotationRuleID;
                                skuInfo.CalculatePutawayLocation = ownerInfo.CalculatePutawayLocation;
                                skuInfo.ReceiptValidationID = ownerInfo.ReceiptValidationID;
                                skuInfo.ZoneID = "";
                                skuInfo.PutawayLoc = "Stage";
                                skuInfo.ReceiptInspectionLoc = "QC";
                                skuInfo.QcLocOut = !string.IsNullOrEmpty(ownerInfo.DefaultQcLocOut) ? ownerInfo.DefaultQcLocOut : "PICKTO";
                                skuInfo.ReturnsLoc = !string.IsNullOrEmpty(ownerInfo.DefaultReturnsLoc) ? ownerInfo.DefaultReturnsLoc : "RETURN";
                                skuInfo.QcLoc = !string.IsNullOrEmpty(ownerInfo.DefaultQcLoc) ? ownerInfo.DefaultQcLoc : "QC";
                                skuInfo.MinQty = 0M;
                                skuInfo.MaxQty = 0M;
                                skuInfo.ReorderPoint = 0;
                                skuInfo.ReorderQty = !string.IsNullOrEmpty(line[10]) ? decimal.Parse(line[10]) : 0M;
                                skuInfo.StdOrderCost = 0f;
                                skuInfo.CarryCost = 0f;
                                skuInfo.Abc = "";
                                skuInfo.CycleCountFrequency = 0;
                                DateTime CS00004 = new DateTime();
                                skuInfo.LastCycleCount = CS00004;
                                skuInfo.CcDiscrepanCyrule = "STD";
                                skuInfo.SkuGroup = "";
                                skuInfo.SkuGroup2 = "";
                                skuInfo.TransportMode = "Parcel";
                                skuInfo.FreightClass = "Heavy";
                                skuInfo.Udf1 = line[11].Trim();
                                skuInfo.Udf2 = line[12].Trim();
                                skuInfo.Udf3 = line[0x16].Trim();
                                skuInfo.Udf4 = line[0x17].Trim();
                                skuInfo.Udf5 = "";
                                skuInfo.Udf6 = "";
                                skuInfo.Udf7 = "";
                                skuInfo.Udf8 = "";
                                skuInfo.Udf9 = "";
                                skuInfo.Udf10 = "";
                                skuInfo.BUdf1 = "";
                                skuInfo.BUdf2 = "";
                                skuInfo.BUdf3 = "";
                                skuInfo.BUdf4 = "";
                                skuInfo.BUdf5 = "";
                                skuInfo.BUdf6 = "";
                                skuInfo.BUdf7 = "";
                                skuInfo.BUdf8 = "";
                                skuInfo.BUdf9 = "";
                                skuInfo.BUdf10 = skuInfo.SkuID;
                                skuInfo.Remark1 = "";
                                skuInfo.Remark2 = "";
                                skuInfo.IsActive = "Y";
                                skuInfo.CreatedBy = "EDI";
                                skuInfo.CreatedDate = DateTime.Now;
                                skuInfo.UpdatedBy = "EDI";
                                skuInfo.UpdatedDate = DateTime.Now;
                                skuDA.InsertSku(dataBase, tran, skuInfo, new List<string> { whLoginID });
                            }
                            VendorInfo vendorInfo = new VendorInfo {
                                VendorID = line[7]
                            };
                            if (vendorDA.CheckVendorIDUnique(dataBase, tran, vendorInfo.VendorID, whLoginID))
                            {
                                vendorInfo = vendorDA.GetVendorByID(dataBase, tran, vendorInfo.VendorID, whLoginID);
                                vendorInfo.Company = line[8].Trim();
                                vendorInfo.UpdatedBy = "EDI";
                                vendorInfo.UpdatedDate = DateTime.Now;
                                vendorDA.UpdateVendor(dataBase, tran, vendorInfo, new List<string> { whLoginID });
                            }
                            else
                            {
                                vendorInfo.Company = line[8].Trim();
                                vendorInfo.Description = "";
                                vendorInfo.Vat = "";
                                vendorInfo.City = "";
                                vendorInfo.State = "";
                                vendorInfo.Zip = "";
                                vendorInfo.Country = "";
                                vendorInfo.CountryCode = "";
                                vendorInfo.Address = "";
                                vendorInfo.Contact1 = "";
                                vendorInfo.Contact2 = "";
                                vendorInfo.Phone1 = "";
                                vendorInfo.Phone2 = "";
                                vendorInfo.Fax1 = "";
                                vendorInfo.Fax2 = "";
                                vendorInfo.Email1 = "";
                                vendorInfo.Email2 = "";
                                vendorInfo.BCity = "";
                                vendorInfo.BState = "";
                                vendorInfo.BCountry = "";
                                vendorInfo.BCountryCode = "";
                                vendorInfo.BAddress = "";
                                vendorInfo.BContact1 = "";
                                vendorInfo.BContact2 = "";
                                vendorInfo.BPhone1 = "";
                                vendorInfo.BPhone2 = "";
                                vendorInfo.BFax1 = "";
                                vendorInfo.BFax2 = "";
                                vendorInfo.BEmail1 = "";
                                vendorInfo.BEmail2 = "";
                                vendorInfo.Udf1 = "";
                                vendorInfo.Udf2 = "";
                                vendorInfo.Udf3 = "";
                                vendorInfo.Udf4 = "";
                                vendorInfo.Udf5 = "";
                                vendorInfo.Udf6 = "";
                                vendorInfo.Udf7 = "";
                                vendorInfo.Udf8 = "";
                                vendorInfo.Udf9 = "";
                                vendorInfo.Udf10 = "";
                                vendorInfo.Remark = "";
                                vendorInfo.IsActive = "Y";
                                vendorInfo.CreatedBy = "EDI";
                                vendorInfo.CreatedDate = DateTime.Now;
                                vendorInfo.UpdatedBy = "EDI";
                                vendorInfo.UpdatedDate = DateTime.Now;
                                vendorDA.InsertVendor(dataBase, tran, vendorInfo, new List<string> { whLoginID });
                                DataSet dsUser = userMstDA.GetAllUserMst(dataBase, tran, ConstUtils.CONST_GLOBAL_TABLE);
                                List<UserVendorInfo> userVendorInfoList = new List<UserVendorInfo>();
                                if (((dsUser != null) && (dsUser.Tables.Count > 0)) && (dsUser.Tables[0].Rows.Count > 0))
                                {
                                    foreach (DataRow userRow in dsUser.Tables[0].Rows)
                                    {
                                        UserVendorInfo userVendorInfo = new UserVendorInfo {
                                            UserID = userRow["USER_ID"].ToString().Trim(),
                                            VendorID = vendorInfo.VendorID,
                                            IsDefault = "N",
                                            CreatedBy = "EDI",
                                            CreatedDate = DateTime.Now,
                                            UpdatedBy = "EDI",
                                            UpdatedDate = DateTime.Now
                                        };
                                        userVendorInfoList.Add(userVendorInfo);
                                    }
                                    userVendorDA.DeleteUserVendorByVendorID(dataBase, tran, vendorInfo.VendorID, ConstUtils.CONST_GLOBAL_TABLE);
                                    userVendorDA.InsertUserVendorByVendorID(dataBase, tran, userVendorInfoList, ConstUtils.CONST_GLOBAL_TABLE);
                                }
                            }
                            AlternateSkuInfo alternateSkuInfo = new AlternateSkuInfo {
                                OwnerID = ownerID,
                                SkuID = line[2].Trim(),
                                AltSkuID = line[6].Trim(),
                                VendorID = line[7].Trim()
                            };
                            if (alternateSkuDA.CheckAlternateSkuIDUnique(dataBase, tran, ownerID, alternateSkuInfo.SkuID, alternateSkuInfo.AltSkuID, alternateSkuInfo.VendorID, whLoginID))
                            {
                                alternateSkuInfo = alternateSkuDA.GetAlternateSkuByID(dataBase, tran, ownerID, alternateSkuInfo.SkuID, alternateSkuInfo.AltSkuID, alternateSkuInfo.VendorID, whLoginID);
                                alternateSkuInfo.PackID = PackID;
                                alternateSkuInfo.DefaultUomID = DefaultUom;
                                alternateSkuInfo.Type = "Vendor";
                                alternateSkuInfo.Udf1 = line[4].Trim();
                                alternateSkuInfo.Udf2 = line[5].Trim();
                                alternateSkuInfo.Udf3 = line[9].Trim();
                                alternateSkuDA.UpdateAlternateSku(dataBase, tran, alternateSkuInfo, new List<string> { whLoginID });
                            }
                            else
                            {
                                alternateSkuInfo.PackID = PackID;
                                alternateSkuInfo.DefaultUomID = DefaultUom;
                                alternateSkuInfo.Type = "Vendor";
                                alternateSkuInfo.Udf1 = line[4].Trim();
                                alternateSkuInfo.Udf2 = line[5].Trim();
                                alternateSkuInfo.Udf3 = line[9].Trim();
                                alternateSkuInfo.Udf4 = "";
                                alternateSkuInfo.Udf5 = "";
                                alternateSkuInfo.Udf6 = "";
                                alternateSkuInfo.Udf7 = "";
                                alternateSkuInfo.Udf8 = "";
                                alternateSkuInfo.Udf9 = "";
                                alternateSkuInfo.Udf10 = "";
                                alternateSkuInfo.IsActive = "Y";
                                alternateSkuInfo.CreatedBy = "EDI";
                                alternateSkuInfo.CreatedDate = DateTime.Now;
                                alternateSkuInfo.UpdatedBy = "EDI";
                                alternateSkuInfo.UpdatedDate = DateTime.Now;
                                alternateSkuDA.InsertAlternateSku(dataBase, tran, alternateSkuInfo, new List<string> { whLoginID });
                            }
                        }
                    }
                    else
                    {
                        result = result + string.Format("仓库:{0},货主:{1}不存在.", whName, ownerID);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                result = result + string.Format(" Error:{0}", ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public bool UpdateMessageCommand(EdiLogInfo ediLogInfo)
        {
            bool result = false;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (ediLogDA.UpdateEdiLog(dataBase, tran, ediLogInfo) > 0)
                {
                    result = true;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateOrdersEdiFlag(List<string> orderIDs, string ownerID, string dbLoginID)
        {
            string ids = "";
            foreach (string orderID in orderIDs)
            {
                ids = ids + "'" + orderID + "',";
            }
            ids = ids.Substring(0, ids.Length - 1);
            string sql = string.Format("UPDATE {0}ORDERS SET EDI_SYNC_FLG = 'Y' WHERE ORDER_ID IN ({1})", dbLoginID + ".", ids);
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql);
        }
    }
}

